<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="isAudit">
        <el-select v-model="queryParams.isAudit" placeholder="请选择审核状态" clearable size="small">
           <el-option
                v-for="dict in auditOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户编号" width="120" align="center" prop="customerCode">
      </el-table-column>
      <el-table-column label="客户名称" align="center" prop="customerName" :show-overflow-tooltip="true">
         <template slot-scope="scope">
           <el-link @click="openCustomer(scope.row)" :underline="false" type="primary" >{{scope.row.customerName}}</el-link>
        </template>
      </el-table-column>
      <!-- <el-table-column label="手机" align="center" prop="mobile" />
      <el-table-column label="电话" align="center" prop="telephone" />
      <el-table-column label="网址" align="center" prop="website" />
      <el-table-column label="邮箱" align="center" prop="email" /> -->
      <el-table-column label="负责人" align="center" prop="userNickName" />
      <el-table-column label="提交时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="isAudit" >
        <template slot-scope="scope">
              <el-tag prop="isAudit" v-for="(item, index) in auditOptions"  :type="scope.row.isAudit==1?'success':'danger'"  v-if="scope.row.isAudit==item.dictValue">{{item.dictLabel}}</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditUserNickName" />
       <el-table-column label="审核时间" align="center" prop="auditTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.auditTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
              </template>
            </el-table-column>
      
      <el-table-column label="审核意见" align="center" prop="auditNote" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  fixed="right" width="80">
        <template slot-scope="scope">
          <el-button
          v-if="scope.row.isAudit==0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row)"
            v-hasPermi="['crm:customer:audit']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-log"
            @click="handleShow(scope.row)"
            v-hasPermi="['crm:customer:query']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
     <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
         
              <el-form-item label="客户名称" prop="customerName">
                <span>{{form.customerName}}</span>
              </el-form-item>
              <el-form-item label="负责人" prop="userNickName">
                <span>{{form.userNickName}}</span>
              </el-form-item>
              <el-form-item label="提交时间" prop="customerName">
                <span>{{parseTime(form.createTime, '{y}-{m}-{d} {h}:{i}:{s}')}}</span>
              </el-form-item>
              <el-form-item label="状态">
                <el-radio-group v-model="form.isAudit">
                  <el-radio  key="1" label="1">通过</el-radio>
                  <el-radio  key="2" label="2">拒绝</el-radio>
                </el-radio-group>
              </el-form-item>
          </el-row>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.auditNote" type="textarea" placeholder="请输入内容" />
              </el-form-item>
            </el-col>
          </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-drawer
     size="50%"
     :with-header="false"
      :title="show.title" :visible.sync="show.open"
      >
        <customer  ref="customer" />
    </el-drawer>
  </div>
</template>

<script>
import { auditListCustomer,auditCustomer  } from "@/api/crm/customer";
import { getToken } from "@/utils/auth";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import customer from "../../components/customerDetails";
export default {
  name: "Customer",
  components: { Treeselect,customer },
  data() {
    return {
      show:{
        open:false,
        title:'客户详情'
      },
      auditOptions:[],
      markersArray:[],
      map:{
        open:false,
        title:'地图选择'
      },
       // 导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/crm/customer/importData",
      },
      deptOptions:[],
      statusOptions:[],
      receiveOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: null,
        mobile: null,
        telephone: null,
        website: null,
        email: null,
        createUserId: null,
        address: null,
        location: null,
        detailAddress: null,
        lng: null,
        lat: null,
        status: null,
        isReceive: null,
        deptId: null,
        isDel: null
      },
      form:{
        customerUserId:null,
        customerId:null,
        userId:null,
        isAudit:'1',
        customerName:null,
        userNickName:null,
        createTime:null,
        auditNote:null,

      },
      rules:{}
      
    };
  },
  created() {
    this.getList();
    this.getDicts("customer_user_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getDicts("customer_user_audit").then((response) => {
      this.auditOptions = response.data;
    });
    this.getDicts("customer_receive").then((response) => {
      this.receiveOptions = response.data;
    });
  },
  mounted() {
   
  },
  methods: {
     openCustomer(row){
      this.show.open=true;
      const customerId = row.customerId || this.ids
      setTimeout(() => {
        this.$refs.customer.getCustomerDetails(customerId);
      }, 500);
    },
    handleAudit(row){
      const customerId = row.customerId || this.ids
      this.open = true;
      this.title = "审核客户";
      this.form.customerUserId=row.customerUserId;
      this.form.customerId=row.customerId;
      this.form.userId=row.userId;
      this.form.customerName=row.customerName;
      this.form.userNickName=row.userNickName;
      this.form.createTime=row.createTime;
    },
    handleShow(row) {
      this.show.open=true;
      const customerId = row.customerId || this.ids
      setTimeout(() => {
        this.$refs.customer.getCustomerDetails(customerId);
      }, 500);
      
    },
    /** 查询客户列表 */
    getList() {
      this.loading = true;
      auditListCustomer(this.queryParams).then(response => {
        this.customerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
     
     
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.customerId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    submitForm() {
       auditCustomer(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("操作成功");
                this.open = false;
                this.getList();
              }
            });
    },
    cancel() {
      this.open = false;
     
    },
    
  }
};
</script>
<style scoped>
 
</style>

